<%--
  Created by IntelliJ IDEA.
  User: ZhuChunXiao
  Date: 2017/5/25
  Time: 15:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<div id="page-content">
  <div class="row">
    <%--<button id="aaa">123</button>--%>
    <div class="col-lg-12">
      <div class="panel">
        <div class="panel-heading">
          <h3 class="panel-title">培训管理</h3>
        </div>
        <div class="panel-body">

          <div class="form-inline">
            <div class="row">
              <div class="col-sm-12 table-toolbar-left">
                <btn id="btn_add_train" class="btn btn-primary btn-sm" ${isCreate}>
                  <i class="ti-plus"> </i> 新建培训申请表
                </btn>
              </div>
            </div>
          </div>
          
          <div class="table-responsive">
            <table id="train_running_table"></table>
            <div id="train_running_page"></div>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>

<script>

  $(function(){

    init_grid_train();

  });

  function init_grid_train() {

    __init_jqgrid('train_running_table', 'train_running_page',
            '<%=path%>/com/train/list?viewtype=${viewtype}',
            ['培训主题', '培训类型', '培训周期', '提交日期', '文档状态', '操作', 'id'],
            [
              {name: 'theme', index: 'theme', width: 60, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'type', index: 'type', width: 80, sortable: false, search: true, stype: 'text', searchoptions: {sopt: ['cn']}},
              {name: 'date', index: 'date', width: 120, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'createTime', index: 'createTime', width: 100, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'state', index: 'state', width: 80, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'act', index: 'act', width: 250, fixed: true, sortable: false, search: false, formatter: actFormat},
              {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,true
    );

  }

  function actFormat(cellvalue, options, rowObject){
    var id = rowObject.id;
    var viewtype="${viewtype}";
    cellvalue = '<button class="btn btn-white btn-sm" data-original-title="查看" onclick="fn_train_read(\'' + id + '\');"><i class="fa fa-eye"> </i> 查看</button>';
    if(viewtype==2){
      cellvalue += '<button class="btn btn-white btn-sm" data-original-title="培训完成" onclick="fn_train_finish(\'' + id + '\');"><i class="fa fa-check"> </i> 培训完成</button>';
      cellvalue += '<button class="btn btn-white btn-sm" data-original-title="培训取消" onclick="fn_train_cancel(\'' + id + '\');"><i class="fa fa-close"> </i> 培训取消</button>';
    }
    return cellvalue;
  }

  $("#btn_add_train").click(function(){
    loadURL('<%=path%>/com/train/input', $('#page-content'));
  });

  function fn_train_read(id) {
    loadURL('<%=path%>/com/train/read?keyId='+id, $('#page-content'));
  }

  //培训完成
  function fn_train_finish(id) {
    __ajax_get('<%=path%>/com/train/checkUser/'+id,null,function(data){
      if(data.ok){
        var url = '<%=path%>/com/train/finish/'+id;
        __open_dialog_form('培训完成', url, function (dialogRef) {
          var callback = function (data) {
            if (data) {
              dialogRef.close();

              // 刷新jqgrid
              $('#train_running_table').jqGrid().setGridParam({datatype:'json'}).trigger('reloadGrid');
            } else {
              dialogRef.enableButtons();
            }
          };
          if (fn_train_finish_save(callback) === false) {
            dialogRef.enableButtons();
          }
        });
      }else{
        alert(data.message);
      }
    });
  }

  //培训取消
  function fn_train_cancel(id) {
    __ajax_get('<%=path%>/com/train/checkUser/'+id,null,function(data){
      if(data.ok){
        var url = '<%=path%>/com/train/cancel/'+id;
        __open_dialog_form('培训取消', url, function (dialogRef) {
          var callback = function (data) {
            if (data) {
              dialogRef.close();

              // 刷新jqgrid
              $('#train_running_table').jqGrid().setGridParam({datatype:'json'}).trigger('reloadGrid');
            } else {
              dialogRef.enableButtons();
            }
          };
          if (fn_train_cancel_save(callback) === false) {
            dialogRef.enableButtons();
          }
        });
      }else{
        alert(data.message);
      }
    });
  }

  $("#aaa").click(function(){
    alert("s");
    $("#train_running_table").trigger("reloadGrid");
  });

</script>